package com.bookpavilion.chat.controller;

import com.bookpavilion.chat.pojo.Message;
import com.bookpavilion.chat.pojo.SystemMessage;
import com.bookpavilion.chat.service.MessageService;
import com.netflix.eureka.util.StatusUtil;
import entity.Result;
import entity.StatusCode;
import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.login.CredentialNotFoundException;
import java.util.List;

@RestController
@RequestMapping("/mess")
public class MessageController {

    @Autowired
    private MessageService messageService;


    @GetMapping("/test")
    public ObjectId test(@RequestBody ObjectId objectId){
        System.out.println(objectId);
        ObjectId id = new ObjectId("asd");
        return id;
    }


    @GetMapping("/all")
    public List<Message> get(){
        return messageService.findAll();
    }

    /**
     * 查询对话最新消息
     * @param fromId
     * @param toId
     * @return
     */
    @GetMapping("/last")
    public Message queryLastMessage(@RequestParam String fromId,@RequestParam String toId){
        return messageService.queryLastMessage(fromId,toId);

    }
    /**
     * 查询对话记录
     * @param fromId
     * @param toId
     * @param page
     * @param size
     * @return
     */
    @GetMapping
    public List<Message> queryMessageList(@RequestParam("fromId")String fromId,@RequestParam("toId")String toId,@RequestParam Integer page,@RequestParam Integer size){
        return messageService.queryMessageList(fromId,toId,page,size);
    }

    /**
     * 删除聊天记录
     * @param fromId
     * @param toId
     */
    @DeleteMapping
    public void deleteMessageList(@RequestParam("fromId")String fromId,@RequestParam("toId")String toId){
        messageService.deleteMessageList(fromId,toId);
    }

    @PostMapping("/getUnReadMsgList")
    public Result getUnReadMsgList(String acceptUserId){
        if (StringUtils.isNotBlank(acceptUserId)){
            return new Result(false, StatusCode.ERROR,"获取未读信息失败");
        }
        List<Message> unReadMsgList = messageService.getUnReadMsgList(acceptUserId);
        return new Result(true,StatusCode.OK,"获取未读信息成功",unReadMsgList);
    }
    @GetMapping("/sendSystemMsg")
    public Result sendSystemMsg(@RequestBody SystemMessage systemMessage){
        messageService.sendSystemMsg(systemMessage);
        return new Result(true,StatusCode.OK,"系统信息发送成功");
    }

}
